//
//  SHPEmoticon.swift
//  weibo
//
//  Created by songhuipan on 16/4/24.
//  Copyright © 2016年 songhuipan. All rights reserved.
//

import UIKit

class SHPEmoticon: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
//        SHPaaa()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI(){
        backgroundColor = UIColor.whiteColor()
        addSubview(emoticonBottom)
        addSubview(keyboardCollectionView)
        addSubview(pageControl)
        keyboardCollectionView.snp_makeConstraints { (make) -> Void in
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(emoticonBottom.snp_top)
            
        }
        // 注意pageControl是添加到self上的，所以不用考虑相对keyboardCollectionView的约束问题
        pageControl.snp_makeConstraints { (make) -> Void in
//            make.leading.trailing.equalTo(self)
            make.centerX.equalTo(self)
            make.bottom.equalTo(keyboardCollectionView)
        }
        
        emoticonBottom.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.bottom.equalTo(self)
            make.height.equalTo(37)
        }
    }
    // MARK:懒加载pageControl
    lazy var pageControl:UIPageControl = {
        let pageControl = UIPageControl()
        pageControl.numberOfPages = SHPEmoticonTool.sharedTools.emoticonGroup[0].count
//        pageControl.currentPageIndicatorTintColor = UIColor.redColor()
//        pageControl.pageIndicatorTintColor = UIColor.grayColor()
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        return pageControl
        
    }()
    // MARK:懒加载自定义中间表情键盘
     lazy var keyboardCollectionView:SHPKeyboardCollectionView = {
       let view = SHPKeyboardCollectionView()
        view.delegate = self
        return view
    }()
    // MARK:懒加载底部控件
    private lazy var emoticonBottom:SHPEmoticonBottom = {
        let bottom = SHPEmoticonBottom()
        bottom.bottomCloseure = {[weak self] (type:ButtonType) in
            
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            self?.keyboardCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            self!.pageControl.numberOfPages = SHPEmoticonTool.sharedTools.emoticonGroup[indexPath.section].count
            self!.pageControl.currentPage = 0
            
        }
        return bottom
    }()
}
extension SHPEmoticon:UICollectionViewDelegate{
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        
        let x = scrollView.contentOffset.x + kScreenWidth/2
        
        let cells = keyboardCollectionView.visibleCells().sort { (cellOne, cellTwo) -> Bool in
            return cellOne.frame.origin.x < cellTwo.frame.origin.x
        }
        if cells.count == 2{
            var indexPath:NSIndexPath?
            let firstCell = cells.first! 
            let lastCell = cells.last!
            
            if firstCell.frame.contains(CGPointMake(x, 0)){
                indexPath = keyboardCollectionView.indexPathForCell(firstCell)
            }else{
                indexPath = keyboardCollectionView.indexPathForCell(lastCell)
            }
            
            emoticonBottom.setCurrentSelectedButton(indexPath!.section)
            pageControl.numberOfPages = SHPEmoticonTool.sharedTools.emoticonGroup[indexPath!.section].count
            pageControl.currentPage = indexPath!.item
        }
    }
}











